/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package http;

/**
 * An exception representing an abnormal termination of an HTTP operation.
 * These are usually only thrown for 4xx and 5xx stati, although tht need
 * not always be the case.
 */
public class Status extends Exception
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/* constructor */
	public Status(int Code, String StatusLine) {
		assert(Code>=100);
		assert(Code<=199);

		code = Code;
		statusLine = StatusLine;
		messageBody = "".getBytes();
		location = null;
	}

	public Status(int Code, String StatusLine, 
		      byte[] MessageBody, java.net.URI Location) {
		assert(Code>=100);
		assert(Code<=599);

		code = Code;
		statusLine = StatusLine;
		messageBody = MessageBody;
		location = Location;
	}

	public Status(int Code, String StatusLine, 
		      String MessageBody, java.net.URI Location) {
		assert(Code>=100);
		assert(Code<=599);

		code = Code;
		statusLine = StatusLine;
		messageBody = MessageBody.getBytes();
		location = Location;
	}

	public String toString() {
		return "Status Exception: "+code+" "+statusLine+" "+messageBody+" "+location;
	}
	
	public Status(int Code, String StatusLine,
		      java.io.InputStream is, java.net.URI Location) 
	throws java.io.IOException {
		assert(Code>=100);
		assert(Code<=599);
		
		code = Code;
		statusLine = StatusLine;
		messageBody = http.Client.drainToByteArray(is);
		location = Location;
	}

    public int GetCode() { return code; }
    public String GetStatusLine() { return statusLine; }
    public java.net.URI GetLocation() { return location; }

	public byte[] GetPayload() { return messageBody; }

    private int code;
    private String statusLine;
    private byte[] messageBody;
    private java.net.URI location;

    public static Status Continue()
    { return new InformationalStatus(100, "Continue"); }
    public static Status SwitchingProtocols()
    { return new InformationalStatus(101, "Switching Protocols"); }
    public static Status Processing()
    { return new InformationalStatus(199, "Processing"); }

    public static Status BadRequest(String message)
    { return new Status(400, "Bad Request", message, null); }
    public static Status Forbidden(String message)
    { return new Status(403, "Forbidden", message, null); }
    public static Status NotFound(String message)
    { return new Status(404, "Not Found", message, null); }
    public static Status MethodNotAllowed(String message)
    { return new Status(405, "Method Not Allowed", message, null); }
    public static Status NotAcceptable(String message)
    { return new Status(406, "No Acceptable Media Type Available", message, null); }
    public static Status Conflict(String message)
    { return new Status(409, "Conflict", message, null); }
    public static Status Gone(String message)
    { return new Status(410, "Gone", message, null); }
    public static Status LengthRequired(String message)
    { return new Status(411, "Request Body Requires Content-Length", message, null); }
    public static Status UnsupportedMediaType(String message)
    { return new Status(415, "Unsupported Media Type in Request Body", message, null); }
    public static Status ExpectationFailed(String message)
    { return new Status(417, "Expectation Failed", message, null); }
	public static Status InternalClientError(String message)
	{ return new Status(499, "Internal Client Error", message, null); }
    public static Status InternalServerError(String message)
    { return new Status(500, "Internal Server Error", message, null); }
    public static Status NotImplemented(String message)
    { return new Status(501, "Operation Not Implemented", message, null); }
    public static Status BadGateway(String message)
    { return new Status(502, "Bad Gateway", message, null); }
    public static Status TemporarilyUnavailable(String message)
    { return new Status(503, "Resource Temporarily Unavailable", message, null); }
}
